package mulMan.domain;

import java.io.File;
import java.io.IOException;
import java.util.Observable;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "AUDIO_FILE")
public class AudioFile extends Observable{
	
	private int idAudioFile;
	private String title;
	private String artist;
	private String album;
	private int length;
	private String path;
	
	public AudioFile() {
		
	}
	
	//Getters and Setters
	@Id
	@GeneratedValue
	@Column(name = "ID_AUDIO_FILE")
	public int getIdAudioFile() {
		return idAudioFile;
	}
	public void setIdAudioFile(int idAudioFile) {
		this.idAudioFile = idAudioFile;
		setChanged();
		notifyObservers("id");
	}
	@Column(name = "TITLE")
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
		setChanged();
		notifyObservers("title");
	}
	@Column(name = "ARTIST")
	public String getArtist() {
		return artist;
	}
	public void setArtist(String artist) {
		this.artist = artist;
		setChanged();
		notifyObservers("artist");
	}
	@Column(name = "ALBUM")
	public String getAlbum() {
		return album;
	}
	public void setAlbum(String album) {
		this.album = album;
		setChanged();
		notifyObservers("album");
	}
	@Column(name = "LENGTH")
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
		setChanged();
		notifyObservers("length");
	}
	@Column(name = "PATH")
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
		setChanged();
		notifyObservers("path");
	}
	
	public String toString() {
		return title + " - " + artist;
	}
	
	public void play() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
		//URL url = getClass().getResource(path);
		//File soundFile = new File(url.getPath());

		File soundFile = new File(path);
	    AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);
	    
	    DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
	    Clip clip = (Clip) AudioSystem.getLine(info);
	    clip.open(sound);
	    
	    clip.start();
	}
}
